// API 测试脚本
// 在浏览器控制台中运行此脚本来测试所有API

const BASE_URL = 'http://localhost:3000/api/v1';

async function testAPI(method, url, data = null) {
  const config = {
    method,
    headers: {
      'Content-Type': 'application/json',
    },
  };
  
  if (data) {
    config.body = JSON.stringify(data);
  }
  
  try {
    const response = await fetch(`${BASE_URL}${url}`, config);
    const result = await response.json();
    console.log(`${method} ${url}:`, result);
    return result;
  } catch (error) {
    console.error(`Error ${method} ${url}:`, error);
    return null;
  }
}

async function runTests() {
  console.log('🚀 开始测试 API...\n');
  
  // 1. 测试登录
  console.log('1. 测试用户登录');
  const loginResult = await testAPI('POST', '/auth/login', {
    login: 'admin',
    password: '123456'
  });
  
  // 2. 测试获取用户列表
  console.log('\n2. 测试获取用户列表');
  await testAPI('GET', '/user/list?page=1&pageSize=10');
  
  // 3. 测试获取试题列表
  console.log('\n3. 测试获取试题列表');
  await testAPI('GET', '/question/list?page=1&pageSize=10');
  
  // 4. 测试获取试卷列表
  console.log('\n4. 测试获取试卷列表');
  await testAPI('GET', '/paper/list?page=1&pageSize=10');
  
  // 5. 测试获取考试活动列表
  console.log('\n5. 测试获取考试活动列表');
  await testAPI('GET', '/exam/list?page=1&pageSize=10');
  
  // 6. 测试创建试题
  console.log('\n6. 测试创建试题');
  await testAPI('POST', '/question/create', {
    content: '这是一个测试题目',
    type: 'Select',
    options: [
      { symbol: 'A', content: '选项A' },
      { symbol: 'B', content: '选项B' },
      { symbol: 'C', content: '选项C' },
      { symbol: 'D', content: '选项D' }
    ],
    answer: [0],
    score: 2,
    difficulty: '简单',
    creator: '507f1f77bcf86cd799439011'
  });
  
  console.log('\n✅ API 测试完成！');
}

// 运行测试
runTests();
